﻿/// <summary>
/// Developer: CHOOI KAH WAI
/// Last Modified: 21/11/2009
/// </summary>

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ARSS.Editor.Common;
using System.IO;
using System.Windows.Markup;
using System.Windows.Media.Media3D;
using System.Diagnostics;

namespace ARSS.Editor
{
	/// <summary>
	/// Interaction logic for SceneNodeWindow.xaml
	/// </summary>
	public partial class SceneNodeWindow : Window
    {
        #region Functions

        /// <summary>
        /// Constructor
        /// </summary>
        public SceneNodeWindow()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.

            Loaded += new RoutedEventHandler(SceneNodeWindow_Loaded);
		}

        /// <summary>
        /// Window loaded event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SceneNodeWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Setup SceneNodeTreeView binding
            System.Collections.ObjectModel.ObservableCollection<SceneNode> nodes = new System.Collections.ObjectModel.ObservableCollection<SceneNode>();
            nodes.Add(Core.Instance.SceneData.Root);
            SceneNodeTreeView.ItemsSource = nodes;
        }

        /// <summary>
        /// User changed the scene node selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SceneNodeTreeView_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
        {
            PropertiesCanvas.DataContext = null;
            PropertiesCanvas.DataContext = e.NewValue;
        }

        /// <summary>
        /// Scene Node Apply Changes Button Clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ApplyButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SceneNode targetNode = PropertiesCanvas.DataContext as SceneNode;

            targetNode.Name = NameTextBox.Text;
            targetNode.ModelPath = ModelFileTextBox.Text;

            Vector3D vec;

            // Translation
            vec = new Vector3D(Double.Parse(TranslationXTextBox.Text), Double.Parse(TranslationYTextBox.Text), Double.Parse(TranslationZTextBox.Text));
            targetNode.Translation = vec;

            // Scale
            vec = new Vector3D(Double.Parse(ScaleXTextBox.Text), Double.Parse(ScaleYTextBox.Text), Double.Parse(ScaleZTextBox.Text));
            targetNode.Scale = vec;

            // Rotation
            vec = new Vector3D(Double.Parse(RotationXTextBox.Text), Double.Parse(RotationYTextBox.Text), Double.Parse(RotationZTextBox.Text));
            targetNode.Rotation = vec;

            // Reload scene nodes
            Core.Instance.PreviewWindowObject.ReloadScene();
        }

        /// <summary>
        /// Delete button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SceneNode targetNode = PropertiesCanvas.DataContext as SceneNode;

            if (targetNode == Core.Instance.SceneData.Root)
            {
                MessageBox.Show("Could not delete root scene node.");
                return;
            }
            else
                targetNode.ParentNode.Children.Remove(targetNode);

            // Reload SceneNodeTreeView
            PropertiesCanvas.DataContext = null;
            SceneNodeTreeView.ItemsSource = null;
            System.Collections.ObjectModel.ObservableCollection<SceneNode> nodes = new System.Collections.ObjectModel.ObservableCollection<SceneNode>();
            nodes.Add(Core.Instance.SceneData.Root);
            SceneNodeTreeView.ItemsSource = nodes;

            // Reload scene nodes
            Core.Instance.PreviewWindowObject.ReloadScene();
        }

        /// <summary>
        /// Add child node button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (PropertiesCanvas.DataContext == null) return;

            NewChildNodeWindow ncnWindow = new NewChildNodeWindow();

            SceneNode parentNode = PropertiesCanvas.DataContext as SceneNode;

            Nullable<bool> result = ncnWindow.ShowDialog();

            if (result.HasValue)
            {
                if (result.Value == true)
                {
                    // Create new scene node
                    SceneNode node = new SceneNode();

                    node.ParentNode = parentNode;

                    node.Name = ncnWindow.NameTextBox.Text;
                    node.ModelPath = ncnWindow.ModelFileTextBox.Text;

                    Vector3D vec;

                    // Translation
                    vec = new Vector3D(Double.Parse(ncnWindow.TranslationXTextBox.Text),
                                        Double.Parse(ncnWindow.TranslationYTextBox.Text),
                                        Double.Parse(ncnWindow.TranslationZTextBox.Text));
                    node.Translation = vec;

                    // Scale
                    vec = new Vector3D(Double.Parse(ncnWindow.ScaleXTextBox.Text),
                                        Double.Parse(ncnWindow.ScaleYTextBox.Text),
                                        Double.Parse(ncnWindow.ScaleZTextBox.Text));
                    node.Scale = vec;

                    // Rotation
                    vec = new Vector3D(Double.Parse(ncnWindow.RotationXTextBox.Text),
                                        Double.Parse(ncnWindow.RotationYTextBox.Text),
                                        Double.Parse(ncnWindow.RotationZTextBox.Text));
                    node.Rotation = vec;

                    parentNode.Children.Add(node);

                    // Reload SceneNodeTreeView
                    PropertiesCanvas.DataContext = null;
                    SceneNodeTreeView.ItemsSource = null;
                    System.Collections.ObjectModel.ObservableCollection<SceneNode> nodes = new System.Collections.ObjectModel.ObservableCollection<SceneNode>();
                    nodes.Add(Core.Instance.SceneData.Root);
                    SceneNodeTreeView.ItemsSource = nodes ;

                    // Reload scene nodes
                    Core.Instance.PreviewWindowObject.ReloadScene();
                }
            }
        }

        /// <summary>
        /// Browse button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BrowseButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "Xaml documents |*.xaml";

            Nullable<bool> result = dialog.ShowDialog();

            if (result == true)
            {
                string filePath = dialog.FileName.Substring(
                    System.IO.Path.GetDirectoryName(
                        Process.GetCurrentProcess().MainModule.FileName).Length + 1);

                ModelFileTextBox.Text = filePath.Replace('\\', '/');
            }
        }

        #endregion
    }
}